/*
 * @Author: yangzonglong
 * @Date: 2021-05-07 10:55:54
 * @version: v1.0.0
 * @Descripttion: 工作流历史
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-09-08 13:57:26
 * @Auditor:
 */
import React, { useMemo, useEffect } from 'react';
import { Table } from '@library';
import { columns } from './data';
import { historyTask } from './api';
import { useState } from 'react';

interface AuditHistoryProps {
  businessId?: string | null;
  workFlowInstanceId?: string | null;
  onLoad?: () => void;
}

export default ({ businessId, workFlowInstanceId, onLoad }: AuditHistoryProps) => {
  const [dataSource, setDataSource] = useState([]);

  const query = useMemo(() => {
    const where: Record<string, any> = {};
    if (businessId) where.businessId = businessId;
    else where.WorkFlowInstanceId = workFlowInstanceId;

    return { where, order: [['createdAt', 'ASC']] };
  }, [businessId, workFlowInstanceId]);

  useEffect(() => {
    historyTask(query).then((res) => {
      setDataSource(res.rows);
      onLoad?.();
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [query]);

  return <Table flat dataSource={dataSource} columns={columns} />;
};
